package com.sphd.emanu.activity.pointscenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.adapter.PointscenterAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.vip.MemPoints;
import com.sphd.emanu.pojo.vip.Member;
import com.sphd.emanu.pojo.vip.MemberForOrg;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 积分中心
 *
 * @Author Hu
 * @Time 2017/6/29 -- 11:01.
 * @Email 87683202@qq.com
 */
public class PointscenterActivity extends MVPBaseActivity<PointscenterContract.View, PointscenterPresenter> implements PointscenterContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.point_show)
    TextView pointShow;
    @BindView(R.id.btn_convert_record)
    TextView btnConvertRecord;
    @BindView(R.id.btn_give)
    TextView btnGive;
    @BindView(R.id.btn_give_record)
    TextView btnGiveRecord;
    @BindView(R.id.all_count)
    TextView allCount;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Intent intent;
    Bundle bundle;
    Member member;
    User user;
    PointscenterAdapter pointscenterAdapter;
    View coupView;
    AlertDialog coupDialog;
    TextView title;
    MaterialEditText myEdit;
    Button btnConfirm, btnCancle;
    int type = 0;
    List<Moudle> moudles;
    boolean isCanDo = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pointscenter);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {

        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                addData(type);
            }
        });
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                coupDialog.dismiss();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(myEdit.getText().toString().trim())) {
                    toast("请输入正确的积分后提交。");
                } else {
                    showProgressDialog();
                    mPresenter.getMemberPoint(member.getId(), user.getOid(), Integer.valueOf(myEdit.getText().toString().trim()));
                }
            }
        });


    }

    private void initView() {
        setTitleBar(toolbar, "积分中心");
        intent = getIntent();
        bundle = intent.getExtras();
        member = (Member) bundle.getSerializable(Constant.MEMBER_ENTITY);
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_VIP_6);
        setRefresh(mySwf);
        initDialog();
        initAdapter();
        addData(0);
    }

    private void initDialog() {
        coupView = View.inflate(context, R.layout.dialog_edit_myself, null);
        title = (TextView) coupView.findViewById(R.id.title);
        myEdit = (MaterialEditText) coupView.findViewById(R.id.my_edit);
        btnConfirm = (Button) coupView.findViewById(R.id.btn_confirm);
        btnCancle = (Button) coupView.findViewById(R.id.btn_cancle);
        coupDialog = getDialogBuilder(coupView, R.style.AlertDialog);
        title.setText("赠送积分");
        myEdit.setHint("请输入赠送积分值");

    }

    private void initAdapter() {
        setRecy(myRecycler);
        pointscenterAdapter = new PointscenterAdapter(R.layout.item_point_center, new ArrayList<MemPoints>());
       pointscenterAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(pointscenterAdapter);
    }

    private void addData(int staus) {
        showProgressDialog();
        mPresenter.findMemberPoint(member.getId(), user.getOid(), staus);
    }

    @OnClick({R.id.btn_convert_record, R.id.btn_give_record, R.id.btn_give})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_convert_record:
                type = 0;
                addData(0);
                break;
            case R.id.btn_give_record:
                type = 1;
                addData(1);
                break;
            case R.id.btn_give:
                if (isCanDo)
                    coupDialog.show();
                break;
        }
    }

    @Override
    public void recBean(MemberForOrg memberForOrg, int status) {
        dissProgressDialog();
        List<MemPoints> pointsList = memberForOrg.getMemPoints();
        pointShow.setText("剩余积分：" + String.valueOf(memberForOrg.getSurplus()));
        L.e(memberForOrg.getOrderCount() + "-----" + memberForOrg.getOrderSum());
        allCount.setText((status == 0 ? "共兑换" : "共获取积分") + memberForOrg.getOrderCount() + "次，共" + (status == 0 ? "兑换" : "获取")
                + memberForOrg.getOrderSum() + "积分");
        pointscenterAdapter.setStatus(status);
        pointscenterAdapter.setNewData(pointsList);
    }

    @Override
    public void recRes(int res) {
        dissProgressDialog();
        if (res == 1) {
            toast(R.string.up_success);
            coupDialog.dismiss();
            type = 1;
            addData(1);
        } else {
            toast(R.string.up_failure);
        }
    }
}
